#include "allocate.h"

/** @fn     char* allocate(char * pos, int x, int size)
    @brief  allocates mem and stores a string within the allocated space
	@param	pos     pointer of source (string)
	@param  x       the amount of characters
	@param  size    size of the string
	@return	pointer of the reserved memory space
    */
char* allocate(char *pos, int x, int size){
    if(DEBUG){printf("\nallocate (%d)", size);}

    if(!size || !pos){
        if(DEBUG){printf("...invalid input");}
        return(NULL);
    }

    char* ptr = NULL;
    int y;

    if(DEBUG){printf("..");}

    //malloc the necessary space
    if((ptr = (char *) malloc((size_t) size)) == NULL){
        if(DEBUG){printf(".fail");}
        return((char *) NULL);
    }else{
        if(DEBUG){printf(".");}
        for(y = 0; y < x; y++){
            // write passed character to reserved memory space
            *(ptr+y) = *(pos+y);
        }
        if(DEBUG){printf(".ok");}
        return(ptr);
    }
}
